package cn.wolfcode.car.business.web.controller.business;


import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.ibatis.type.LongTypeHandler;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {

    //模板前缀
    private static final String prefix = "business/system/statementItem/";
    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IStatementService statementService;
    @RequestMapping("/itemDetail")
    public String itemDetail(Model model,Long statementId){
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            model.addAttribute("statement",statementService.get(statementId));
            return prefix+"itemDetail";
        }else {
            model.addAttribute("statement",statementService.get(statementId));
            model.addAttribute("");
            return prefix+"itemEdit";
        }
    }
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo query(StatementItemQuery qo){
        TablePageInfo<StatementItem> info = statementItemService.query(qo);
        return info;
    }
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> list){
            statementItemService.saveList(list);
            return AjaxResult.success();
    }
    @RequestMapping("/payFun")
    @ResponseBody
    public AjaxResult payFun(Long statementId){
        statementService.pay(statementId);
        return AjaxResult.success();
    }
    @RequestMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response,Long statementId) throws IOException {
        //1. 设置响应下载响应头
        response.setHeader("Content-Disposition","attachment;filename=statementItem.xlsx");
        //将数据写出到Work
        Workbook wb=statementService.export(statementId);
        wb.write(response.getOutputStream());
        wb.close();
    }
}
